from machine import Pin 
import time

#初始化引脚，引脚在此修改
keydown = Pin(13, Pin.IN, Pin.PULL_UP)
keyup = Pin(2, Pin.IN, Pin.PULL_UP)
keyleft = Pin(27, Pin.IN, Pin.PULL_UP)
keyright = Pin(35, Pin.IN, Pin.PULL_UP)
keyA = Pin(34, Pin.IN, Pin.PULL_UP)
keyB = Pin(12, Pin.IN, Pin.PULL_UP)


#先打印引脚的状态
print("GPIO_13:", keydown.value())
print("GPIO_2:", keyup.value())
print("GPIO_27:", keyleft.value())
print("GPIO_35:", keyright.value())
print("GPIO_34:", keyA.value())
print("GPIO_12:", keyB.value())


# 定义去抖动函数
def debounce(pin):
    state = pin.value()
    time.sleep(0.01)
    # 确定引脚状态是否与初始状态相同
    return state == pin.value()

while True:
    # 检查按键是否稳定地被按下
    if debounce(keydown) and keydown.value() == 0:
        # 如果按键被按下
        if not keydown.value():
            # 当按键没有被释放时，循环等待
            while not keydown.value():            
                pass
        print("下按键", keydown.value())

    if debounce(keyup) and keyup.value() == 0:
        # 如果按键被按下
        if not keyup.value():
            # 当按键没有被释放时，循环等待
            while not keyup.value():            
                pass
        print("上按键", keyup.value())

    if debounce(keyleft) and keyleft.value() == 0:
        if not keyleft.value():
            while not keyleft.value():            
                pass
        print("左按键", keyleft.value())
    if debounce(keyright) and keyright.value() == 0:
        if not keyright.value():
            while not keyright.value():            
                pass
        print("右按键", keyright.value())
    if debounce(keyA) and keyA.value() == 0:
        if not keyA.value():
            while not keyA.value():            
                pass
        print("A按键", keyA.value())
    if debounce(keyB) and keyB.value() == 0:
        if not keyB.value():
            while not keyB.value():            
                pass
        print("B按键", keyB.value())
    time.sleep(0.01)